package com.sojoys.artifact.pool;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月20日 下午2:59:09
 * @Description ：Please describe this document
 */
public class StringBuilderFactory implements PooledObjectFactory<StringBuilder> {

	
	static final Log log = LogFactory.get(StringBuilderFactory.class);
	/**
	 * 创建一个新对象;当对象池中的对象个数不足时,将会使用此方法来"输出"一个新的"对象",并交付给对象池管理
	 */
	@Override
	public PooledObject<StringBuilder> makeObject() throws Exception {
		log.debug("ObjectPool -> {}","makeObject");
		return new DefaultPooledObject<StringBuilder>(new StringBuilder());
	}

	/**
	 * 销毁对象
	 */
	@Override
	public void destroyObject(PooledObject<StringBuilder> p) throws Exception {
		log.debug("ObjectPool -> {}","destroyObject");
		StringBuilder sb = p.getObject();
		sb = null;
	}

	/**
	 * 验证对象
	 */
	@Override
	public boolean validateObject(PooledObject<StringBuilder> p) {
		log.debug("ObjectPool -> {}","validateObject");
		return p.getObject() != null;
	}

	/**
	 * 激活对象
	 */
	@Override
	public void activateObject(PooledObject<StringBuilder> p) throws Exception {
		log.debug("ObjectPool -> {}","activateObject");
	}

	/**
	 * 钝化对象
	 * 将资源返还给资源池时，调用此方法。
	 */
	@Override
	public void passivateObject(PooledObject<StringBuilder> p) throws Exception {
		log.debug("ObjectPool -> {}","passivateObject");
		StringBuilder sb = p.getObject();
		sb.setLength(0);
	}

}
